package com.xyc.student.controller;

import com.xyc.student.mapper.AssociateMapper;
import com.xyc.student.mapper.StuMapper;
import com.xyc.student.mapper.TeacherMapper;
import com.xyc.student.pojo.Associate;
import com.xyc.student.pojo.Stu;
import com.xyc.student.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class FanController {

    @Autowired
    StuMapper stuMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    AssociateMapper associateMapper;

    //关注页
    @RequestMapping("/fans")
    public String fans(Model model) { //待获取要操作的学生的id
        List<Stu> stuList = stuMapper.find();
        model.addAttribute("stu", stuList); //学生信息表 List<Stu>

        List<Teacher> teacherList = teacherMapper.find();
        String[] teaName = new String[60];
        int k = 0;
        for (Teacher t : teacherList) {
            teaName[k++] = t.getName(); // 老师的名字 String[]
            System.out.print(teaName[k - 1] + " ");
        }

        model.addAttribute("tea", teacherList); // 老师信息表 List<tea>
        model.addAttribute("teaName", teaName);

        //查询张老师的粉丝
        List<Associate> associateList = associateMapper.find();
        System.out.println("关联表查询结果:" + associateList);
        model.addAttribute("associateList", associateList);
        return "/stu/fans";
    }

    @RequestMapping("/attentionYes/{stu_id}")
    public String yes(@PathVariable("stu_id") Integer stu_id) {
        //修改成1
        System.out.println("yes修改的学生id:" + stu_id);
        associateMapper.updateYes(stu_id);
        return "redirect:/fanss";
    }

    @RequestMapping("/attentionNo/{stu_id}")
    public String no(@PathVariable("stu_id") Integer stu_id) {
        //修改成0
        System.out.println("no修改的学生id:" + stu_id);
        associateMapper.updateNo(stu_id);
        return "redirect:/fanss";
    }

    @GetMapping("/fanss")
    public String fanss(Model model) {
        List<Associate> associateList = associateMapper.find();
        model.addAttribute("associateList", associateList);
        return "/stu/fans";
    }

    @RequestMapping("/FansReturn")
    public String ret() {
        return "redirect:/stus";
    }

    @RequestMapping("/myFans")
    public String myFans(Model model) {
        List<Associate> fanList = associateMapper.findById(1);
        model.addAttribute("fanList", fanList);
        return "fanList";
    }
}
